package com.fadadianqi.logistics.admin.net;

import java.io.UnsupportedEncodingException;

/**
 * 客户端和服务端文本通信将继承此类
 * 此类的包体为可转化为JSON对象的字符串
 *
 * @author aroot
 */
public abstract class JSONPacket extends Packet {

    /**
     *
     */
    private static final long serialVersionUID = -6965657775853185949L;

    private PacketHead head;
    private byte[] bodyBytes = null;

    protected JSONPacket(PacketHead head, String json) {
        this.json = json;
        this.head = head;
    }

    private String json;

    public byte[] getBodyBytes() {
        if (json != null) {
            try {
                bodyBytes = json.getBytes(CHARSET);
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
        }
        return bodyBytes;
    }

    public PacketHead getHead() {
        return head;
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        sb.append(this.getClass().getSimpleName());
        sb.append(", json: " + json);
        return sb.toString();
    }
}
